package com.csdj.t309.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileOwnerAttributeView;

@RequestMapping("/system")
@Controller
public class UploadController {

    @GetMapping("/upload")
    public String upload(){
        return "upload";
    }

    @PostMapping("/upload")
    public String doUpload(@RequestParam MultipartFile[] files,
                           @RequestParam String name,
                           @RequestParam int age) throws IOException {

        for(MultipartFile file : files){
            String fileType = file.getName();
            System.out.println(fileType);
            String filename = file.getOriginalFilename();
            String ext = filename.substring(filename.lastIndexOf("."));
            long curTime = System.currentTimeMillis();
            Path path = Paths.get("e:", "upload");
            if(!Files.exists(path)){
                Files.createDirectory(path);
            }
            file.transferTo(Paths.get("e:","upload",String.valueOf(curTime)+ext));

        }
        return "success";
    }
}
